function getChallanBranchName(str,c){
        var xmlhttp;
        if (str=="") {
            document.getElementById("challan_branch"+c).innerHTML="None";
            return;
        }
        if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        }
        else {// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange=function() {
            if (xmlhttp.readyState==4 && xmlhttp.status==200) {
              //  document.getElementById("txtHint1").innerHTML=xmlhttp.responseText;
                document.getElementById("challan_branch"+c).innerHTML=xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET","populateChallanBranchName.do?method=populateBranchName&q="+str+"&var="+c,true);
        xmlhttp.send();
}

var index = 1;
function increment() {
           index++;
    // document.getElementById('button1').value=index;
           return index;
}
function decrement() {
           if(index<=1)
               index=1;
           else
               index--;
           // document.getElementById('button1').value=index;
        return index;
}
var count=1;
function addRow() {
           if(checkPaymentNull()){
           count=increment();
           // alert(count1);
           var c=document.getElementById('dataTable').rows.length;
           var x=document.getElementById('dataTable').insertRow(c);
           var y=x.insertCell(0);
           var z=x.insertCell(1);
           var p=x.insertCell(2);
           var q=x.insertCell(3);
           var r=x.insertCell(4);//style="width: 85px"

           y.innerHTML="<input type='text' name='challan_date' id='challan_date"+count+"' style='width: 85px' readonly onchange='dateChecking(challan_date"+count+")'/>"+
                       "&nbsp;<img alt=\"\" src=\"Images/cal/cal.gif\" style=\"cursor: pointer;\" onclick=\"displayCalendar(document.getElementById('challan_date"+count+"'),'dd/mm/yyyy',this)\"/>";

           z.innerHTML = "<input type='text' id='challan_no"+count+"' name='challan_no' style='width: 110px;text-align:left;' onblur=\"validateChallan_no('challan_no"+count+"')\"/>";
           p.innerHTML = "<select id='challan_bank_name"+count+"' name='challan_bank_name' style='width: 210px;text-align:left;' onchange='getChallanBranchName(this.value,"+count+")'>"+
                              " <option value='0'>Select</option>"+
                              " <logic:notEmpty name='bank'>"+
                                    "<logic:iterate id='dat' name='bank'>"+
                                       " <option value='${dat.bank_cd}'>${dat.bank_name}</option>"+
                                    "</logic:iterate>"+
                              " </logic:notEmpty>"+
                         "</select></div>";
            q.innerHTML = "<div align='left' id='challan_branch"+count+"'><select name='challan_bank_branch' id='challan_bank_branch"+count+"' style='width: 210px;text-align:left;'>"+
                              "<option value='0'>Select</option>"+
                              "</select></div>";
            r.innerHTML="<div align='right'><input type='text' id='challan_amount"+count+"' name='challan_amount' style='width: 110px;text-align: right' value='0.00' onkeyup=\"zeroBeforePoint('challan_amount"+count+"')\" onblur=\"demoMatchClick('challan_amount"+count+"');CheckDecimal('challan_amount"+count+"');checkPaymentDuplicate();\"/></div>";
              //  document.getElementById("form1").vat_add.disabled=true;
               //counter(count);
               return true;
           }
           else
               return false;
}
function validateChallan_no(id) {
          var Str=document.getElementById(id).value;
          if(/^\d{1,16}$/.test(Str)) {
             return true;
          }
          else {
              alert("Not Valid!!");
              document.getElementById(id).value="";
              document.getElementById(id).focus();
              return false;
          }
}
function checkPaymentNull(){
         var flag=false;
         for(var i=count;i>0;i--){//alert(count+"hello"+i)
            if(document.getElementById("challan_date"+i).value=="" ||
                document.getElementById("challan_no"+i).value=="" ||
                 document.getElementById("challan_no"+i).value=="0" ||
                  document.getElementById("challan_bank_name"+i).value=="0" ||
                   document.getElementById("challan_bank_branch"+i).value=="0" ||
                    document.getElementById("challan_amount"+i).value=="0" ||
                     document.getElementById("challan_amount"+i).value=="0.0" ||
                      document.getElementById("challan_amount"+i).value=="0.00"){
                       alert(" Please Enter all Payment Details");
                       flag= false;break;
            }
            else
                flag= true;
         }
          return flag;
}
function checkPaymentDuplicate(){
           bool=true;
           var i=0;
           i=count;
           var x=0,y=0;
           for(x=i;x>0;x--){
              for( y=1;y<=x-1; y++){
                  if((document.getElementById("challan_date"+y).value==document.getElementById("challan_date"+x).value) &&
                          (document.getElementById("challan_no"+y).value==document.getElementById("challan_no"+x).value) &&
                              (document.getElementById("challan_bank_name"+y).value==document.getElementById("challan_bank_name"+x).value) &&
                                  (document.getElementById("challan_bank_branch"+y).value==document.getElementById("challan_bank_branch"+x).value)){
                      alert("Repetation Of Challan No. And Challan Date Of Same Branch Of Same Bank Not Allowed");
                      bool=false;break;
                  }
                  else bool=true;
              }if(bool==false)break;
           }
           return bool;
}

function deleteRow(tableID) {
        var tbl = document.getElementById(tableID);
        var lastRow = tbl.rows.length;//alert(lastRow +"  "+count)
        if (lastRow > 2) {
            tbl.deleteRow(lastRow - 1);
            decrement();
            count--;
        }
      //  document.getElementById("form1").vat_add.disabled=true;
     //   document.getElementById("form1").vat_del.disabled=true;
}



